package com.app.community.service;

import com.app.community.mapper.RegisterMapper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author SakuraA6
 * @Description 注册用户的业务逻辑层
 * @Date 2020/10/14 20:28
 **/
@Service
public class RegisterService {

    @Autowired
    RegisterMapper registerMapper;

    /**
     * 注册用户
     */
    public void registerUser(String userId,String password){
        // password加密
        Object pass = new SimpleHash("MD5", password,ByteSource.Util.bytes(userId),2);

        // 注册给用户提供默认头像以及默认nickName
        String avatar = "https://hgshequ-bucket.oss-cn-shanghai.aliyuncs.com/default/default_avatar/%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8FICON.png";
        // nickName不能太长
        String nickName = "社区" + (UUID.randomUUID().toString()).substring(0,8);
        registerMapper.registerUser(userId,pass.toString(),avatar,nickName);
    }

    public boolean isUserExist(String userId){
       Integer integer =  registerMapper.isUserExist(userId);
        return integer != null;
    }


}
